package com.uaroadshow.finale.web.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.utils.JsonUtils;
import com.demo2do.core.web.handler.CookieHandler;
import com.demo2do.core.web.utils.WebUtils;
import com.demo2do.core.wechat.entity.cfg.JsConfig;
import com.uaroadshow.entity.user.User;
import com.uaroadshow.finale.Constant;
import com.uaroadshow.finale.processor.WechatProcessor;
import com.uaroadshow.finale.service.UserService;
import com.uaroadshow.finale.web.interceptor.UserContext;
import com.uaroadshow.finale.web.interceptor.UserContextType;
import com.uaroadshow.finale.web.resolver.Secure;

/**
 * 
 * @author mohanlan
 *
 */
@Controller
@RequestMapping("")
public class HomeController {
    
    private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
    
    @Autowired
    private WechatProcessor wechatProcessor;
    
    @Autowired
    private CookieHandler cookieHandler;
    
    @Autowired
    private UserService userService;
    
    /**
     * 
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    @UserContext(type = UserContextType.WECHAT_AUTHORITY)
    public ModelAndView index(@Secure User user, HttpServletRequest request) {
        
        ModelAndView modelAndView = new ModelAndView("index");
        
        String curl = WebUtils.getRealRequestURL(request);
        JsConfig jsConfig = wechatProcessor.generateJsConfig(curl);
        modelAndView.addObject("jsConfig", JsonUtils.toJson(jsConfig));
        
        modelAndView.addObject("user", user);
        
        return modelAndView;
        
    }
    
    /**
     * 
     * @param request
     * @return
     */
    @RequestMapping(value = "/bind-mobile", method = RequestMethod.GET)
    @UserContext(type = UserContextType.WECHAT_AUTHORITY)
    public ModelAndView bindMobile(@Secure User user, HttpServletRequest request) {
        
        ModelAndView modelAndView = new ModelAndView("profile/bind-mobile");
        
        String curl = WebUtils.getRealRequestURL(request);
        JsConfig jsConfig = wechatProcessor.generateJsConfig(curl);
        modelAndView.addObject("jsConfig", JsonUtils.toJson(jsConfig));
        
        modelAndView.addObject("user", user);
        
        return modelAndView;
        
    }
    
    /**
     * 
     * @return
     */
    @RequestMapping("/wechat-required")
    public String wechatRequired() {
        return "wechat-required";
    }
    
    /**
     * 
     * @return
     */
    @RequestMapping("/error")
    public String error() {
        return "error-page";
    }
    
    /**
     * clear cookie
     * 
     * @param response
     * @return
     */
    @RequestMapping(value = "/clear", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody String clear(@Secure User user, HttpServletResponse response) {
        
        try {
            
            // remove use cookie
            cookieHandler.removeCookie(response, Constant.OPENID_KEY);
            cookieHandler.removeCookie(response, Constant.UNIONID_KEY);
            cookieHandler.removeCookie(response, Constant.USER_ACCOUNT_KEY);
            cookieHandler.removeCookie(response, Constant.MOBILE_KEY);
            
            // update user
            userService.update(user.editMobile(null));
            
            logger.trace("#clear() - clear success");
            return "success";
            
        } catch (Exception e) {
            logger.error("#clear() - clear fail, exception={}", e);
            return "fail";
        }
        
    }
    
}
